package org.example.learn.javaagent.bytebuddy;

import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

// 计算方法执行耗时
public class MethodCostTime {

    @RuntimeType
    public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable) {
        long start = System.currentTimeMillis();
        try {
            // 原方法执行
            return callable.call();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.println(method + "方法执行耗时：" + (System.currentTimeMillis() - start) + "ms");
        }
        return null;
    }
}
